package tcp.通过tcp实现通信;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

//客户端
public class TheClient {
    public static void main(String[] args) {
        InetAddress inetAddress = null;
        Socket socket = null;
        OutputStream os = null;
        try {
            //1.获取地址和端口号
            inetAddress = InetAddress.getByName("127.0.0.1");
            int dk = 9999;
            //2.创建一个socket连接
            socket = new Socket(inetAddress, dk);//通过构造方法方法连接
            //3.发送消息 IO流
            os = socket.getOutputStream();//字节输出流
            os.write("客户端".getBytes());//发送消息


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
}
